package com.google.gsaguide.gsa;

import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class represents a Google Plus user and contains his information.
 * @author Felipe Porge Xavier - http://www.felipeporge.com
 */
public class Ambassador {

    private String id;
    private String name;
    private String university;
    private String location;
    private String profileURL;
    private String image;

    /**
     * Constructor method - Generate automatically the profileURL using the Ambassador ID.
     * @param id - Ambassador's Google Plus ID.
     * @param university - Ambassador's university.
     * @param location - Ambassador's location.
     */
    public Ambassador(String id, String university, String location) {
        this.setId(id);
        this.setUniversity(university);
        this.setLocation(location);
    }

    public Ambassador(){}

    /**
     * This method allows to get the stored user's profile url.
     *
     * @return - User's profile URL.
     */
    public String getProfileURL() {
        return profileURL;
    }

    /**
     * This method allows change the user's profile url.
     *
     * @param profileURL - New user's profile URL.
     */
    public void setProfileURL(String profileURL) {
        this.profileURL = profileURL;
    }

    /**
     * This method allows to get the stored user's id.
     *
     * @return - User's id.
     */
    public String getId() {
        return id;
    }

    /**
     * This method allows change the user's id.
     *
     * @param id - New user's id.
     */
    public void setId(String id) {
        this.id = id;
        if (isNumeric(id))
            this.profileURL = "http://plus.google.com/" + id;
        else
            this.profileURL = "http://plus.google.com/+" + id;
    }

    /**
     * This method allows to get the stored user's photo url.
     *
     * @return - User's photo URL.
     */
    public String getPhotoURL() {
        return image;
    }

    /**
     * This method allows change the user's photo url.
     *
     * @param photoURL - New user's photo URL.
     */
    public void setPhotoURL(String photoURL) {
        this.image = photoURL;
    }

    /**
     * This method allows to get the stored user's name.
     *
     * @return - User's name.
     */
    public String getName() {
        return name;
    }

    /**
     * This method allows change the user's name.
     *
     * @param name - New user's name.
     */
    public void setName(String name) {
        this.name = applyCharsetCorrection(name);
    }

    /**
     * This method allows to get the stored user's university.
     *
     * @return - User's university.
     */
    public String getUniversity() {
        return this.university;
    }

    /**
     * This method allows change the user's university.
     *
     * @param university - New user's university.
     */
    public void setUniversity(String university) {
        this.university = university;
    }

    /**
     * This method allows to get the stored user's location.
     *
     * @return - User's location.
     */
    public String getLocation() {
        return location;
    }

    /**
     * This method allows change the user's location.
     *
     * @param location - New user's location.
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * This method convert this ambassador to String.
     * @author Jordan Lira
     * @return - String with all the Ambassador params.
     */
    @Override
    public String toString() {
        return "Ambassador{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", university='" + university + '\'' +
                ", location='" + location + '\'' +
                ", profileURL='" + profileURL + '\'' +
                ", image='" + image + '\'' +
                '}';
    }

    /**
     * This method verify the string charset and apply a correction if the charset is invalid.     *
     * @param str - String to test.
     * @return - Corrected string.
     */
    public String applyCharsetCorrection(String str){
        if(isValidName(str))
            return str;

        try {
            String newStr = new String(str.getBytes("ISO-8859-1"));
            Log.w(getClass().getSimpleName(), "Charset Correction: " + str + " >>>> " + newStr);
            return newStr;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * This method verifies if this string is a valid name.     *
     * @param name - String to test.
     * @return - True if is valid and False if isn't valid.
     */
    private boolean isValidName(String name) {
        Pattern defaultPatt = Pattern.compile("[a-z A-ZãÃáÁàÀêÊéÉèÈíÍìÌôÔõÕóÓòÒúÚùÙûÛçÇ.']*");
        Matcher search = defaultPatt.matcher(name);

        if (search.matches()) {
            return true;
        }

        return false;
    }

    /**
     * This method returns if the String is numeric.     *
     * @param str - String to test.
     * @return - Return true if "str" is numeric.
     */
    private static boolean isNumeric(String str) {
        try {
            Double.parseDouble(str);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
}
